#include <nemesis.functions.h>
#include <nemesis.Logger.h>

#include <nemesis.app.functions.h>
#include <nemesis.app.Application.h>

#include <nemesis.io.functions.h>

#include <nemesis.dbms.Database.h>
#include <nemesis.dbms.Statement.h>

#include <nemesis.dbms.oracle.Database.h>

#include <nemesis.dbos.Repository.h>

#include <dbos_demo.oodb.Table03.h>

using namespace std;
using namespace nemesis;
using namespace dbos_demo;

oodb::Table03::Loader* oodb::Table03::st_loader = NULL;

dbos_prepare_object (oodb::Table03);

//------------------------------------------------------------------------------------------
// El tema viene de el conjunto de tendr� que haber contenido objetos de la clase
// business::Table03, pero a diferencia de la template db::Object en el db::Set no he
// podido interceptar la creacin de objetos => en el conjunto slo pueden haber db::Table03
//
// As�que cargo los service uno a uno, creando business::Table03 y los guardo en un
// vector normal y corriente.
//// (1) Observar que la factor� de objetos que usa es la de business::Table03.
//------------------------------------------------------------------------------------------
void oodb::Table03::setup (dbms::Database& database, dbos::Repository& repository)
   throw (RuntimeException)
{
   st_loader = new Table03::Loader (database);

   Table03::setStorageArea (
      repository.createStorageArea (                                                  // (1)
         Table03::getStorageAreaId (), Table03::getStorageAreaName (), Table03::getMaxSize (),
         Table03::allocator, 2
      )
   );
}

oodb::Table03* oodb::Table03::instanciate (dbms::Connection& connection, const int id)
   throw (RuntimeException)
{
   if (st_loader == NULL)
      throw RuntimeException ("smart::oodb::Table03::setup no ha sido invocado", FILE_LOCATION);

   Table03* result = NULL;

   try {
      Guard guard (st_loader, "smart::oodb::Table03::Loader");
      result = dbos::ObjectFacade <Table03>::instanciate (connection, st_loader->setKey (id));
   }
   catch (dbms::DatabaseException& edb) {
      throw RuntimeException (edb);
   }

   return result;
}

//------------------------------------------------------------------------------------------
// Inicializa cada una de las subscripciones encontradas en la BD.
//
// (1) Carga las aplicaciones cliente asociadas a �ta subscripcin.
// (2) Separa las aplicaciones registradas como sondas y/o interactivas.
//------------------------------------------------------------------------------------------
void oodb::Table03::initialize (dbos::Loader& loader)
   throw (RuntimeException, dbms::DatabaseException)
{
   Table03::Loader& dbLoader = static_cast <Table03::Loader&> (loader);
   a_id = dbLoader.getId ();
   a_name = dbLoader.getName ();
   
   LOGINFORMATION (
      string msg ("dbos_demo::oodb::Table03::initialize | Id: ");
      msg += nemesis::functions::asString (a_id);
      Logger::information (msg, FILE_LOCATION);
   );
}

void oodb::Table03::destroy ()
   throw ()
{
   LOGINFORMATION (
      string msg ("dbos_demo::oodb::Table03::destroy | Id: ");
      msg += nemesis::functions::asString (a_id);
      Logger::information (msg, FILE_LOCATION);
   );
}

string oodb::Table03::asString () const
   throw ()
{
   std::string result ("smart::oodb::Table03 { Id: ");
   result += nemesis::functions::asString (a_id);
   result += " | Nombre: ";
   result += a_name;
   return result += " }";
}

//--------------------------------------------------------------------------------
// Definicin del cargador
//--------------------------------------------------------------------------------
dbms::Statement* oodb::Table03::Loader::initialize (dbms::Database& database)
   throw (RuntimeException)
{
   dbms::Statement* statement = database.createStatement (
      "table03", "select text from nemesis_db_table03 where key=:id"
   );
   statement->bindInput ("id", a_id);
   statement->bindOutput ("name", a_name);
   return statement;
}

string oodb::Table03::Loader::asString () const
   throw ()
{
   std::string result ("smart::oodb::Loader::Table03 { Id: ");
   result += nemesis::functions::asString (a_id);
   return result += " }";
}

